Using the useReducer Hook in React with TypeScript

05.18.2021

The useReducer hook is an alternative to the useState hook and is preferable when you have complex state logic or when your next state depends on your previous state.

The useReducer hook accepts a reducer type (state, action) => newState and returns a state object paired with a dispatch method much like Redux.

Now the official useReducer documentation will show you how to define a reducer that accepts actions you will call with a dispatch method.

That is a good use case but I have another example of using useReducer to provide partial updates to state which I find covers the rest of my use cases.

useReducer with actions and a dispatch method

The default example for useReducer is set up for you to create a reducer function and provide it with an action. The action provided should have a type and some value to update the state.

To strongly type this feature with TypeScript we can create an enum with all of our possible action types as well as create an interface for the action.

Below is a fairly contrived example but shows the most basic example possible.

// An enum with all the types of actions to use in our reducer
enum CountActionKind {
  INCREASE = 'INCREASE',
  DECREASE = 'DECREASE',
}

// An interface for our actions
interface CountAction {
  type: CountActionKind;
  payload: number;
}

// An interface for our state
interface CountState {
  count: number;
}

// Our reducer function that uses a switch statement to handle our actions
function counterReducer(state: CountState, action: CountAction) {
  const { type, payload } = action;
  switch (type) {
    case CountActionKind.INCREASE:
      return {
        ...state,
        value: state.count + payload,
      };
    case CountActionKind.DECREASE:
      return {
        ...state,
        value: state.count - payload,
      };
    default:
      return state;
  }
}

// An example of using the `useReducer` hooks with our reducer function and an initial state
const Counter: FunctionComponent = () => {
  const [state, dispatch] = useReducer(counterReducer, { count: 0 });
  return (
    <div>
      Count: {state.count}
      {/* Calling our actions on button click */}
      <button
        onClick={() => dispatch({ type: CountActionKind.INCREASE, payload: 5 })}
      >
        -
      </button>
      <button onClick={() => dispatch({ type: CountActionKind.DECREASE, payload: 5 })}>+</button>
    </div>
  );
};

useReducer using the Partial type for state updates

The next way of using useReducer is best used when you have a state object where you need to change some values inside of it directly rather than dispatching an action to handle state changes.

This way also has fewer types but does expose the state to be changed directly so take that into account when using this method.

// Interface for our state
interface LoadingState {
  loaded: boolean;
  loading: boolean;
  error: Error | null;
}

// The example component that use the `useReducer` hook with our state
const LoadingComponent: FunctionComponent = () => {
  /** 
   See here that we are using `newState: Partial<LoadingState>` in our reducer
   so we can provide only the properties that are updated on our state
   */
  const [state, setState] = useReducer(
    (state: LoadingState, newState: Partial<LoadingState>) => ({
      ...state,
      ...newState,
    }),
    {
      loaded: false,
      loading: false,
      error: null,
    }
  );

  useEffect(() => {
    // And here we provide only properties that are updating on the state
    setState({ loading: true });
    setState({ loading: false, loaded: true });
    setState({ loading: false, loaded: true, error: new Error() });
  }, []);

  if (state.loading) {
    return <p>Loading</p>;
  }

  return <p>{state}</p>;
};

Conclusion

These are my two methods so check out the other posts below for other methods. It's beneficial to read the wide range of ways to accomplish the same thing.

https://www.newline.co/@bespoyasov/how-to-use-usereducer-with-typescript--3918a332

https://www.sumologic.com/blog/react-hook-typescript/

https://www.benmvp.com/blog/type-checking-react-usereducer-typescript/